﻿
import flash.geom.Point;
import YouHP.Graphics.*;

class Weight extends MultiChartBase
{
	//图表全局数据
	var mGroupCount : Number = 1;		//图表的组数
	var mGroup : Array = new Array();	//图表的组信息
	
	//图例数据部分变量
	var mLegendLeft : Number = 10;				//图例的左边界
	var mLegendOption : Array = new Array();	//图例数据选项
	var mLegendTop : Number = 10;				//图例数据顶部偏移
	var mLegendWidth : Number = 110;			//图例宽度
	
	//其它
	var mGap : Number = 15;						//图表与图例X轴上间隔
	var mTitleColor : Number = 0xff0000;		//图标标题文字颜色
	
	//坐标轴相关数据  
	var mYGap : Number = 25;					//Y轴上的最小间隔
	var mChartTop : Number = 40;				//图表数据顶部的Y轴偏移
	var mChartLeft : Number = mLegendLeft + mLegendWidth + mGap;		//X轴的偏移
	
	//Y轴刻度值
	var mYLabelArr : Array = new Array();
	var mMaxIdxVal = 35, mMinIdxVal = 15;
	
	//图表数据值
	var mDataValueArr : Array = new Array() ;
	var mRadius = 4 ;
	
    function Weight(lv : Object)
    {
        mClip = _root.createEmptyMovieClip("Weight", _root.getNextHighestDepth());
        mData = lv;		
        
		//提取疾病图表公用信息
        ParseBase(lv);
		
		//设置一些私有信息（不用去做解析）
        ParsePrivateData();
		
		//设置图例数据
		SetLegendData();
		
		//画图
        Draw();
    }
	
	private function ParsePrivateData()
	{
		//设置组信息数据(图表名称，刻度数， 图表Y轴顶部， 图表Y轴底部， 有效高度, 最大值， 最小值)
		mGroupCount = 2;
		mGroup.push(["体重（单位：千克）", 4, 0, 0, 0, 0, 200]);
		mGroup.push(["体重指数BMI", 3, 0, 0, 0, mMinIdxVal, mMaxIdxVal]);
		
		//体重值
		mYLabelArr.push(["160"]);
		mYLabelArr.push(["120"]);
		mYLabelArr.push(["80"]);
		mYLabelArr.push(["40"]);
		
		//体重指数
		mYLabelArr.push(["30"]);
		mYLabelArr.push(["25"]);
		mYLabelArr.push(["20"]);
		
		//提取数据值
		var value1Array = mData["Value1"].split(";");
		var value2Array = mData["Value2"].split(";");
		
		mDataValueArr.push(value1Array);
		mDataValueArr.push(value2Array);
	}
	
	private function SetLegendData()
	{
		//数组中内容分别代表：体重指数程度， 体重指数颜色， 体重指数范围
        mLegendOption.push(["肥胖",  0x9DCEFF, 25, mMaxIdxVal]);
        mLegendOption.push(["超重", 0xB9DCFF, 23, 25]);
        mLegendOption.push(["正常",  0xffffff, 18, 23]);
        mLegendOption.push(["偏瘦", 0xDDEEFF, mMinIdxVal, 18]);		
	}
    
    private function Draw()
    {
		//画图例
        DrawLegend();
		
		//画网格线以及坐标值
		DrawGrid();

		//画体重数据
        DrawData();        
    } 
	     
    public function DrawLegend()
    {
      	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);

        var pen = new Pen(1.0,0x000000,100);
		var titlePen = new Pen(1.0,0xcccccc,100);
		var brush = new SolidBrush(0x000000, 100);
        var startx = mLegendLeft + mLegendXGap;
        var starty = 0;
        var y = 0;
		var curLegendTop = mLegendTop;
		
		for(var k=0; k < mGroupCount; k++)
	   	{
			starty = curLegendTop + 5;
			gs.DrawLine(titlePen, mLegendLeft, curLegendTop, mLegendWidth, curLegendTop, 1, 1); 
			DrawHtmlText("<b>" + mGroup[k][0] + "</b>", startx, starty, mTitleColor, 20);
			
			if(k == 1)	//只对第二组数据添加legend说明
			{ 
				starty += 2*mLegendYGap;
        		for(var i=0; i < mLegendOption.length; i++)
        		{
            		y = starty + i * mLegendYGap;
            
            		brush.setColor(mLegendOption[i][1]);
            		gs.FillRect(brush, startx + 10, y, 12, 12);
            		gs.DrawRect(pen, startx + 10, y, 12, 12);
            
           	 		var label:TextField = mc.createTextField("option_" + i ,mc.getNextHighestDepth(),
              			startx + 25, y-3, 0, 0);
            		var myformat = new TextFormat();
        			myformat.color = 0xff0000;
		    		myformat.align = "left";
		    		label.setTextFormat(myformat);
		            
        			label.html = true;
		    		label.autoSize = "left";
		    		label.htmlText = mLegendOption[i][0];
        		}
						
				gs.DrawRect(pen, startx, starty - mLegendYGap/2, mLegendWidth - 4*mLegendXGap, (mLegendOption.length + 0.5) *mLegendYGap);
			}			
			
			curLegendTop = curLegendTop + mYGap * Number(mGroup[k][1] + 2.5);	//计算下一个图表标题显示的纵向位置			
			//判断是否会重叠在图例上，如果是，则需要调整当前的图例顶部
			if(curLegendTop <= starty + mLegendOption.length*mLegendYGap)
			{
				curLegendTop = starty + (mLegendOption.length+0.5)*mLegendYGap
			}
		}        
    }
	
	//根据时间获得相对于体重指数图表顶部的Y轴方向的偏移距离
	private function GetYMigration(groupIdx:Number, idxVal:Number)
	{
		var retVal = 0;

		if(idxVal >= mGroup[groupIdx][6])
		{
			return 0;
		}
		else if(idxVal <= mGroup[groupIdx][5])
		{
			return mGroup[groupIdx][3] - mGroup[groupIdx][2];
		}
		else
		{
			retVal = (mGroup[groupIdx][6] - idxVal)*1.0/(mGroup[groupIdx][6] - mGroup[groupIdx][5])*mGroup[groupIdx][4];
		}			
			
		return retVal ;
	}

    public function DrawGrid()
    {
       	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);		
		var gridPen = new Pen(1.0,0xcccccc,100);
		var brush = new SolidBrush(0x000000, 100); 
		var yTickCnt : Number = 0;
		var xTickCnt : Number = 0;
		var mXGap : Number = 40 ;					
		var tmpY : Number = mChartTop;
		var tmpX : Number = mChartLeft;
		var curGroupTop : Number = mChartTop ;
		var tmpXVal : String ;
		var varArr : Array;
		var textGap = 10;
		var curIdx=0;
		
		//解析X轴坐标
		ParseXCoordinate(mDosageDate, mUpdateDate, mShowMode) ;				

       	for(var i=0; i < mGroupCount; i++)
	   	{			
			yTickCnt = Number(mGroup[i][1]); 
			//画网格的矩形边框
			gs.DrawRect(gridPen, mChartLeft, curGroupTop, mChartWidth, mYGap*(yTickCnt + 1));		
			tmpY = curGroupTop + mYGap ;
			
			for(var j=0; j < yTickCnt; j++)	
			{
				//画横向的网格线
				gs.DrawDashLine(gridPen, mChartLeft, tmpY, mChartLeft + mChartWidth, tmpY, 1, 1);
				//画Y轴坐标值
				DrawText(mYLabelArr[curIdx++][0], mChartLeft - textGap/2.0, tmpY - textGap, mTitleColor, 8, "right");
				
				tmpY += mYGap;
			}			
			
			//保存各图表对应的顶部和底部Y轴偏移
			mGroup[i][2] = curGroupTop;
			mGroup[i][3] = tmpY;
			mGroup[i][4] = tmpY - curGroupTop;						
									
			curGroupTop = tmpY + mYGap ;
	   	}
				
		//画X轴坐标
		for(i=0; i<mChartCoordinate.length; i++)
		{		
			tmpXVal = Utils.DateToXStr(mChartCoordinate[i][0]);
			tmpX = mChartCoordinate[i][1];
			
			DrawHtmlText(tmpXVal, mChartLeft + tmpX - mChartXValidPos, tmpY + 0.5*mYGap, mTitleColor, 8);
		}
		
		//对体重指数图表(第2个图表)进行颜色填充		
		var tmpTop = 0, tmpBottom = 0;
		for(i=0; i<mLegendOption.length; i++)
		{
			tmpTop = GetYMigration(1, mLegendOption[i][3]) ;
			tmpBottom = GetYMigration(1, mLegendOption[i][2]);

			brush.setColor(mLegendOption[i][1]);
			gs.FillRect(brush, mChartLeft,  mGroup[1][2] + tmpTop, mChartWidth, tmpBottom - tmpTop);
		}
		
		//画纵横向的网格线
		xTickCnt = mChartXCount;
		for(var k=1; k < xTickCnt; k++)
		{
			gs.DrawDashLine(gridPen, mChartLeft + k*mChartXGap, mChartTop, mChartLeft + k*mChartXGap, tmpY, 1, 1); 
			tmpX += mXGap;				
		}			
		
		gs.DrawRect(gridPen, mChartLeft, mGroup[1][2], mChartWidth, mGroup[1][4]);		
		
		//画最新更新点的标志线
		var tmpWidth = 1;
		var tmpColor = 0xA0A000;
		gridPen.setColor(tmpColor);
		gs.DrawRect(gridPen, mChartLeft + mChartWidth - tmpWidth/2.0, mLegendTop, tmpWidth, tmpY-mLegendTop);
		DrawHtmlText("<b>最近更新点</b>", mChartLeft+mChartWidth, mLegendTop-textGap, tmpColor, 8);				
    }
	
	private function DrawPoint(pen:Pen, brush:SolidBrush, x:Number, y:Number, radius:Number, value:String, pointCnt:Number)
	{
		var mc:MovieClip = _root.createEmptyMovieClip( "Tip_" + pointCnt, _root.getNextHighestDepth() );
		var gs:Graphics = new Graphics(mc);		
		var link:String = "" ;	//对应的超链接
		
		mc.ToolTip(value, link);
	
		gs.DrawCircle(pen, x, y, radius);
		gs.FillCircle(brush, x, y, radius);		
		
		this.mChildmcArr.push(mc);
	}
	
	private function DrawRectLine(gs:Graphics, pen:Pen, brush:SolidBrush, x1:Number, y1:Number, x2:Number, y2:Number, h:Number)
	{
		var points:Array = new Array();
		var tmpx = (y2-y1)*1.0/(x2-x1)*h ;

		var point1:Point = new Point();
		point1.x = x1 + tmpx;
		point1.y = y1 + h;
		points.push(point1);

		var point2:Point = new Point();
		point2.x = x1 - tmpx;
		point2.y = y1 - h;
		points.push(point2);
		
		var point3:Point = new Point();
		point3.x = x2 - tmpx;
		point3.y = y2 - h;
		points.push(point3);
		
		var point4:Point = new Point();
		point4.x = x2 + tmpx;
		point4.y = y2 + h;
		points.push(point4);

		gs.drawPolygon(pen, points);
		gs.fillPolygon(brush, points);
	}
	
    public function DrawData()
    {
       	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mc);		
		var pen = new Pen(1.0,0x4040c0,100);		
		var brush = new SolidBrush(0x4040c0, 100);

		var dateArray : Array;
		var valueArray : Array;
		var cnt = 0;

		var tmpX:Number = 0, lastTmpX:Number = -1;
		var tmpY:Number = 0;
		var lastTmpY:Array = new Array();
		var tmpValue = 0;
		var pointCnt = 0;
		
		var txtTip:String ;

		dateArray = mData["Time"].split(";");
		pointCnt= dateArray.length;
		
		//不用考虑边界，作为离散点处理
		for(var i=0; i < pointCnt; i++)
	   	{
			tmpX = GetXMigration(Utils.StrToDate(dateArray[i]));
			for(var j=0; j<mGroupCount; j++)
			{
				tmpValue = mDataValueArr[j][i];
				tmpY = GetYMigration(j, tmpValue) ;
				
				//判断是否第一个点，如果不是，则需要连线
				if(i > 0 && lastTmpX >= 0)
				{
					//画线
					DrawRectLine(gs, pen, brush, mChartLeft + lastTmpX, mGroup[j][2] + lastTmpY[j], mChartLeft + tmpX, mGroup[j][2] + tmpY, 
						tmpY >= lastTmpY ? mRadius*(-0.5) : mRadius*(0.5)) ;
				}
				
				//画点
				if(tmpX >= 0)
				{
					txtTip = mGroup[j][0] + "\n" + tmpValue + "\n" + dateArray[i];
					DrawPoint(pen, brush, mChartLeft + tmpX, mGroup[j][2] + tmpY, mRadius, txtTip, cnt++);
				}
				
				lastTmpY[j] = tmpY;
			}
			
			lastTmpX = tmpX;			
		}
    }  	
}